package kpi.java_training.users;

import kpi.java_training.database.MySqlDataManager;
import kpi.java_training.utils.DomDataSaver;
import kpi.java_training.utils.Log;
import kpi.java_training.utils.XmlConstants;


/**
 * Created by Alex Kebkalo (kebka@mail.ru) on 06.12.13.
 * Class implements methods to work with users: authentication, registration
 */
public class UserManager {
    UsersList users;

    public UserManager() {
        try {
            MySqlDataManager loader = new MySqlDataManager();
            users = loader.loadUsers();
        } catch (Exception e) {
            Log.writeToLog(e);
        }
    }

    public boolean userExists(String login) {
        return users.userExists(login);
    }

    public boolean isPasswordCorrect(String login, String password) {
        return users.isPasswordCorrect(login, password);
    }

    public void appendUser(String login, String password) {
        users.addUser(login, password);
        try {
            DomDataSaver domDataSaver = new DomDataSaver(XmlConstants.USERS_FILE);
            domDataSaver.saveUserData(users);
        } catch (Exception e) {
            Log.writeToLog(e);
        }
    }

    public String getOriginalUserName(String userName) {
        return users.getOriginalUserName(userName);
    }

}
